package com.internetCafes.spms.web.sys.service.impl;

import com.internetCafes.spms.core.generic.GenericDao;
import com.internetCafes.spms.core.generic.GenericServiceImpl;
import com.internetCafes.spms.core.model.pageModel.DataGrid;
import com.internetCafes.spms.web.sys.vo.ResourceInfoVo;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.internetCafes.spms.web.sys.dao.ResourceInfoMapper;
import com.internetCafes.spms.web.sys.model.ResourceInfo;
import com.internetCafes.spms.web.sys.service.ResourceInfoService;


/**
 * 资源信息表 ResourceInfoService 实现类
 *
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2019-04-26 15:27:07
 */
@Service("resourceInfoService")
public class ResourceInfoServiceImpl extends GenericServiceImpl<ResourceInfo, Long> implements ResourceInfoService {

    @Autowired
    private ResourceInfoMapper resourceInfoMapper;

    @Value("${system.remote.url}")
    private String remoteUrl;

    /**
     * 批量更新
     *
     * @param resourceBatch 列表
     * @param moduleCode    资源模块code
     * @param modelId       对应记录id
     * @author Zj
     */
    @Override
    public void updateBatch(List<ResourceInfo> resourceBatch, String moduleCode, Long modelId) {
        /* 1-批量删除原来模块内容
         * 2-批量保存内容
         */
        if (CollectionUtil.isEmpty(resourceBatch)) {
            return;
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("moduleId", modelId);
        params.put("moduleCode", moduleCode);
        deleteByModuleAndUserId(params);
        // 保存内容列表
        saveBatch(resourceBatch);
    }

    @Override
    public List<ResourceInfo> query(Map<String, Object> params) {
        List<ResourceInfo> resourceInfoList = resourceInfoMapper.selectByParams(params);
        if (resourceInfoList != null && resourceInfoList.size() > 0) {
            for (ResourceInfo r : resourceInfoList) {
                r.setFileName(r.getPath().replace("pic" + File.separator, ""));
                r.setPath(remoteUrl + r.getPath());
            }
        }
        return resourceInfoList;
    }

    @Override
    public ResourceInfo findById(Long id) {
        return resourceInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ResourceInfo> queryByIds(List<Long> ids) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("ids", ids);
        return resourceInfoMapper.selectByParams(params);
    }

    @Override
    public List<ResourceInfo> page(Map<String, Object> params, int page, int rows) {
        params.put("page", page);
        params.put("rows", rows);
        return resourceInfoMapper.selectByParams(params);
    }

    @Override
    public long count(Map<String, Object> params) {
        return resourceInfoMapper.countByParams(params);
    }

    @Override
    public ResourceInfo findFirst(Map<String, Object> params) {
        List<ResourceInfo> list = resourceInfoMapper.selectByParams(params);
        ResourceInfo resourceInfo = null;
        if (list != null && list.size() > 0) {
            resourceInfo = list.get(0);
        }
        return resourceInfo;
    }

    @Override
    public int save(ResourceInfo resourceInfo) {
        return resourceInfoMapper.insert(resourceInfo);
    }

    @Override
    public int saveBatch(List<ResourceInfo> resourceInfoList) {
        return resourceInfoMapper.insertBatch(resourceInfoList);
    }

    @Override
    public int saveSelective(ResourceInfo resourceInfo) {
        return resourceInfoMapper.insertSelective(resourceInfo);
    }

    @Override
    public int update(ResourceInfo resourceInfo) {
        return resourceInfoMapper.updateByPrimaryKey(resourceInfo);
    }

    @Override
    public int updateSelective(ResourceInfo resourceInfo) {
        return resourceInfoMapper.updateByPrimaryKeySelective(resourceInfo);
    }

    @Override
    public int delete(Long id) {
        return resourceInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        return resourceInfoMapper.deleteBatchByPrimaryKey(ids);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                delete(id);
            }
        }
    }

    @Override
    public GenericDao<ResourceInfo, Long> getDao() {
        return resourceInfoMapper;
    }

    /**
     * DataGrid
     */
    @Override
    public DataGrid dataGrid(Map<String, Object> params, int pageNo, int pageSize) {

        List<ResourceInfo> resourceInfoList = page(params, (pageNo - 1 < 0 ? -1 : pageNo - 1) * pageSize, pageSize);
        long count = count(params);

        DataGrid dg = new DataGrid(resourceInfoList, count, pageSize, pageNo);
        return dg;
    }

    @Override
    public void deleteByModuleAndUserId(Map<String, Object> params) {
        resourceInfoMapper.deleteByModuleAndUserId(params);
    }

    @Override
    public List<ResourceInfoVo> selectByParamsUnionUserInfo(Map<String, Object> params) {
        List<ResourceInfoVo> resourceInfoList = resourceInfoMapper.selectByParamsUnionUserInfo(params);
        if (resourceInfoList != null && resourceInfoList.size() > 0) {
            for (ResourceInfo r : resourceInfoList) {
                r.setFileName(r._getFileName());
                r.setPath(remoteUrl + r.getPath());
            }
        }
        return resourceInfoList;
    }
}